<?php


namespace app\admin\service\base;


use app\admin\model\base\Question;
use app\admin\validates\PaperCateValidate;
use app\admin\validates\QuestionValidate;
use app\BaseService;
use think\exception\ValidateException;
use think\response\Json;

class QuestionService extends BaseService
{
    /**
     * 班级列表
     * @param array $params
     * @return Json
     */
    public function index(array $params): Json
    {
        $where[] = ['is_del','=',0];
        if(isset($params['special_id']) && $params['special_id']){
            $where[] = ['special_id','=',$params['special_id']];
        }
        if(isset($params['name']) && $params['name']){
            $where[] = ['name','like',"%{$params['name']}%"];
        }
        //获取列表
        $data = Question::getList($where, $params['page'] ?? 1, $params['limit'] ?? 100, 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 详情
     * @param array $params
     * @return Json
     */
    public function read(array $params): Json
    {
        //数据验证
        try {
            validate(QuestionValidate::class)->scene('details')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $where = ['id'=>$params['id']];
        $data = Question::where($where)->with(['subjects','special','admin'])->find();
        return jsonReturn(1, $data);
    }

    /**
     * 保存
     * @param array $params
     * @return Json
     */
    public function save(array $params): Json
    {
        //数据验证
        try {
            validate(QuestionValidate::class)->scene('add')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $data = Question::create([
            'name'=>$params['name'],
            'sort'=>$params['sort'] ?? 1,
            'create_user'=>WS_ADMIN_ID,
            'desc'=>$params['desc'] ?? '',
            'special_id'=>$params['special_id'],
            'img'=>$params['img'] ?? ''
        ]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 编辑班级
     * @param array $params
     * @return Json
     */
    public function update(array $params): Json
    {
        //数据验证
        try {
            validate(QuestionValidate::class)->scene('edit')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $data = Question::where('id','=',$params['id'])
            ->update([
                'name'=>$params['name'],
                'sort'=>$params['sort'] ?? 1,
                'create_user'=>WS_ADMIN_ID,
                'desc'=>$params['desc'] ?? '',
                'special_id'=>$params['special_id'],
                'img'=>$params['img'] ?? ''
            ]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 编辑班级
     * @param array $params
     * @return Json
     */
    public function delete(array $params): Json
    {
        //数据验证
        try {
            validate(QuestionValidate::class)->scene('del')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        $data = Question::where('id','=',$params['id'])->update(['is_del'=>1]);
        if($data){
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }
}